<?php
/**
 * 精品推荐模型层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Model
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: GoodsDiggModel.class.php 7402 2011-05-28 16:15:23Z richie $
 * 
 */
class GoodsDiggModel extends CommonModel {
	
	/*
	 * 取精品推荐/同价位商品 等商品
	 * @ param int 		$goods_id	商品ID
	 * @ param int 		$limit		返回商品数量
	*/
	public function getGoods($goods_id,$limit=4){
		if(!is_numeric($goods_id)) return array();
		$cache_name="__digg_goods_{$goods_id}___";
		$diggs=S($cache_name);
		if($diggs) return $diggs;
		
		$digg=$this->field('id,digg_title,digg_goods')->where("goods_id={$goods_id} and is_open=1")->select();
		if($digg){
			$tmp=array();
			foreach($digg as $d){
				$tmp[$d['id']]['digg_title']=$d['digg_title'];
				$ids=explode(',',$d['digg_goods']);
				if(count($ids)>$limit) $ids=array_slice($ids,-$limit);
				foreach($ids as $id){
					$goods=$this->field('goods_id,goods_name,shop_price,goods_thumb,goods_maidian,is_on_sale,is_delete')->table(C('DB_PREFIX').'goods')->where("goods_id={$id}")->find();
					if($goods['is_on_sale']==1 && $goods['is_delete']==0){
						$tmp[$d['id']]['goods'][]=$goods;
					}
				}
			}
			unset($digg,$ids,$goods);
			S($cache_name,$tmp,3600*24);
			return $tmp;
			
		}else return array();
	}
}
?>